import requests
from lxml import etree
import Config
from entity.News import News

# 获取中证网新闻
def getCswNews():
    newsList = []
    headers = {
        'User-Agent':
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    resp = requests.get(url='http://www.cs.com.cn/', headers=headers)
    resp.encoding = 'gbk'
    resp = resp.text
    tree = etree.HTML(resp)

    a_list = tree.xpath(
        '/html//div[@class="box410 ch_focus space_l1"]//a')

    for i in a_list:
        title = i.xpath('./text()')[0].replace("\n", "")
        if len(title) <= 1:  # 如果title为空则继续
            continue

        url = "http://www.cs.com.cn/"+i.xpath('./@href')[0]
        id = url.split('/')[-1].strip('.html')
        news = News("证券日报", title, url, id)
        newsList.append(news.__dict__)

    return newsList
